/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ruoyi.framework.utils;

import net.dreamlu.mica.core.utils.ClassUtil;
import net.dreamlu.mica.core.utils.JsonUtil;
import net.dreamlu.mica.core.utils.WebUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * mica获取参数
 *
 * @author L.cm
 */
public class ServletUtils {

    /**
     * 获取请求参数
     */
    public static Map<String, String> getRequestParams() {
        HttpServletRequest request = WebUtil.getRequest();
        Map<String, String[]> params = request.getParameterMap();
        Map<String, String> params2 = new HashMap<>();
        for (String key : params.keySet()) {
            String[] values = params.get(key);
            if (values.length > 0) {
                params2.put(key, request.getParameter(key));
            }
        }
        return params2;
    }

    /**
     * 获取请求body
     */
    public static String getRequestBody(JoinPoint point) {
        MethodSignature ms = (MethodSignature) point.getSignature();
        Method method = ms.getMethod();
        Object[] args = point.getArgs();
        // 一次请求只能有一个 request body
        Object requestBodyValue = null;
        for (int i = 0; i < args.length; i++) {
            // 读取方法参数
            MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
            RequestBody requestBody = methodParam.getParameterAnnotation(RequestBody.class);
            // 如果是body的json则是对象
            if (requestBody != null) {
                requestBodyValue = args[i];
                break;
            }
        }
        return requestBodyValue == null ? null : JsonUtil.toJson(requestBodyValue);
    }

    /**
     * 构造html页面
     *
     * @return 构造页面
     */
    public static String buildHtml(String script) {
        // 回退页面
        StringBuffer sf = new StringBuffer();
        sf.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=\"UTF-8\"\"/></head><body>");
        sf.append("</body>");
        sf.append("<script type=\"text/javascript\">");
        sf.append(script);
        sf.append("</script>");
        sf.append("</html>");
        return sf.toString();
    }

}
